package edu.biwu.utils;

import cn.hutool.json.JSONUtil;
import edu.biwu.main.java.edu.biwu.ResponseData;

import java.lang.reflect.Method;

public class ResultUtils {

    public static <T> String result(Enum anEnum,T data){

        try {
            ResponseData<T> responseData = new ResponseData<>();
            responseData.setData(data);
            if (anEnum!=null){
                Method getCodeMethod = anEnum.getClass().getMethod("getCode");
                Method getMsgMethod = anEnum.getClass().getMethod("getMsg");
                if (getCodeMethod!=null){
                    responseData.setCode((Integer) getCodeMethod.invoke(anEnum,null));
                }
                if (getMsgMethod!=null){
                    responseData.setMsg((String) getMsgMethod.invoke(anEnum,null));
                }
            }

            return JSONUtil.toJsonStr(responseData);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
